<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/web/header.jsp" %>
<title>修改分类信息</title>
</head>
<body>
<fieldset class="layui-elem-field" style="margin:30px;padding:30px">
	<legend>添加商品信息</legend>
	<div class="layui-field-box">
			<form class="layui-form-pane layui-form" lay-filter="formA">
			
				<div class="layui-form-item">
				<label class="layui-form-label">商品编号</label>
					<div class="layui-input-inline">
						<input class="layui-input" name="code" type="text" placeholder="请输入商品编号"
						required lay-verify="required" autocomplete="off" readonly>
					</div>
				</div>
				
				<div class="layui-form-item">
				<label class="layui-form-label">一级分类</label>
					<div class="layui-input-inline">
						<select name="productType1" lay-verify="required" lay-filter="productType1"></select>
					</div>
				</div>
				
				<div class="layui-form-item">
				<label class="layui-form-label">二级分类</label>
					<div class="layui-input-inline">
						<select name="parentCode" lay-verify="required" ></select>
					</div>
				</div>
				
				<div class="layui-form-item">
					<div class="layui-input-inline">
						<input type="button" value="确定" class="layui-btn" lay-submit lay-filter="updLevel"  />
						<input type="reset" value="重置" class="layui-btn layui-btn-primary" />
					</div>
				</div>
					
			</form>
		</div>
	</fieldset>		
	

<script type="text/javascript">
	getSelect('productType1','type1',init)
	function getSelect(selectid,parentCode,func){
		if(parentCode==''){
			$("select").empty();
			form.render("select");
			return; 
		}
		ajax({parentCode:parentCode},'/cate/getAll.do','json',function(data){
			var html = "<option value=''>-- 请选择 --</option>";
			$.each(data,function(i,dom){
				html += "<option value='"+dom.code+"'>"+dom.name+"</option>"
			});
			$("select[name='"+selectid+"']").html(html);
			form.render("select");
			func();
		})
	}
	
	form.on('select(productType1)',function(data){
		getSelect('parentCode',data.value);
	})
	
	
	function init(){	
		var code = '<%=request.getParameter("code") %>'
			ajax({code:code},'/product/get.do','json',function(data){
				console.log(data)
				$("input[name='code']").val(data.code);
				$("input[name='code']").prop("readonly","readonly")
				$("select[name='productType1'] option[value='"+data.productType1+"']").prop('selected','selected');
					form.val("formA",data)
					getSelect('parentCode',data.productType1,function(){
						$("select[name='parentCode']>option[value='"+data.parentCode+"']").prop('selected','selected');
						form.render('select');
					})
// 				form.val("formA",data)
			})
	}
	
	formOn('submit(updLevel)','/product/updateLevel.do','text',function(data){
		if(1==data){
			layer.msg("修改成功",toClose)
		}else{
			layer.msg("修改失败")
		}
	})
</script>
	
</body>
</html>